package com.wc.算法基础课.C第三讲搜索与图论.BFS.八数码;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

/**
 * @Author congge
 * @Date 2024/3/14 16:40
 * @description https://www.acwing.com/activity/content/punch_the_clock/11/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 9;
    static char[] s = new char[N];
    static String end = "12345678x";
    static Map<String, Integer> d = new HashMap<>();
    static Queue<String> q = new LinkedList<>();
    static int[] dx = new int[]{-1, 1, 0, 0};
    static int[] dy = new int[]{0, 0, -1, 1};

    public static void main(String[] args) {
        for (int i = 0; i < N; i++) {
            s[i] = sc.next().toCharArray()[0];
        }
        bfs();
        out.println(d.getOrDefault(end, -1));
        out.flush();
    }

    static void bfs() {
        String start = new String(s);
        q.add(start);
        d.put(start, 0);
        while (!q.isEmpty()) {
            String t = q.poll();
            if (t.equals(end)) break;
            int k = t.indexOf('x');
            int x = k / 3;
            int y = k % 3;
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (nx >= 0 && nx < 3 && ny >= 0 && ny < 3) {
                    s = t.toCharArray();
                    swap(s, k, nx * 3 + ny);
                    String state = new String(s);
                    if (!d.containsKey(state)) {
                        q.add(state);
                        d.put(state, d.get(t) + 1);
                    }
                    swap(s, k, nx * 3 + ny);
                }
            }
        }
    }

    static void swap(char[] s, int i, int j) {
        char t = s[i];
        s[i] = s[j];
        s[j] = t;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
